package it.polimi.loganalyzer;

import it.polimi.loganalyzer.loader.apache.ApacheTokenizer;

import java.text.ParseException;
import java.util.Date;

public class LogLine {
	
	public static LogLine fromLog(ApacheTokenizer tokenizer, String line) {
		try {
			String ip = tokenizer.extractIp(line);
			String url = tokenizer.extractUrl(line);
			Date date = tokenizer.extractDate(line);
			String referrer = tokenizer.extractReferrer(line);
			LogLine logLine = new LogLine(line, ip, url, date, referrer);
			return logLine;
		} catch(IllegalStateException ex) {
			return null;
		} catch(ParseException ex) {
			return null;
		}
	}
	
	private String rawLine;
	
	private String ip;
	
	private String url;
	
	private Date date;
	
	private String referrer;
	
	public LogLine(String rawLine, String ip, String url, Date date, String referrer) {
		super();
		if(rawLine==null || ip==null || url==null || date == null)
			throw new NullPointerException();
		this.rawLine = rawLine;
		this.ip = ip;
		this.url = url;
		this.date = date;
		this.referrer = referrer; 
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getReferrer() {
		return referrer;
	}

	public void setReferrer(String referrer) {
		this.referrer = referrer;
	}

	public String getRawLine() {
		return rawLine;
	}

	public void setRawLine(String rawLine) {
		this.rawLine = rawLine;
	}
}
